package com.lagou.service.impl;

import com.lagou.dao.AccountDao;
import com.lagou.pojo.Account;
import com.lagou.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author ZHAO Yudong
 * @Date 2020/10/23 16:31
 * @description：
 */

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    public AccountServiceImpl(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    //@Transactional
    public int transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        Account from = accountDao.queryAccountByCardNo(fromCardNo);
        Account to = accountDao.queryAccountByCardNo(toCardNo);

        from.setMoney(from.getMoney() - money);
        to.setMoney(to.getMoney() + money);

        int row1 = accountDao.updateAccountByCardNo(to);
        int c = 1 / 0;
        int row2 = accountDao.updateAccountByCardNo(from);

        return row1 & row2;
    }
}
